/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) 2015  <copyright holder> <email>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "led.h"

led::led(string file)
{
	device_file = file;
	fd = -1;
}
led::~led()
{

}
void led::led_open()
{
	fd = open(device_file.c_str() , O_RDWR | O_NONBLOCK); 
	if (fd < 0)    //打开失败
	{   
		perror("open pwm_buzzer device");
		exit(1);
	}
}
void led::led_close()
{
	if (fd >= 0) 
	{
		close(fd);                   //关闭设备驱动程序文件
		fd = -1;
	}
}
void led::led_on()
{
	unsigned char buf[] = "111111111";
	write(fd,buf,10);
}
void led::led_off()
{
	unsigned char buf[] = "000000000";
	write(fd,buf,10);
}
void led::led_flicker()
{
	int i = 0;
	for(i = 0; i< 3;i++)
	{
		led_on();
		usleep(1000*100);
		led_off();
		usleep(1000*100);
	}
}

void led::led_flicker(unsigned int ms)
{
	int i = 0;
	for(i = 0; i< 2;i++)
	{
		led_on();
		usleep(1000*ms);
		led_off();
		usleep(1000*ms);
	}
}
#if 0
int main()
{
	led led_led1("/sys/devices/platform/x210-led/led4");
	led_led1.led_open();
	led_led1.led_on();
	sleep(1);
	led_led1.led_off();
	sleep(1);
	led_led1.led_flicker(500);
	led_led1.led_close();
	return 0;
}
#endif

